package com.mcxx.modules.base.utils;

import com.alibaba.fastjson.JSON;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.BaseParam;
import com.mcxx.modules.base.service.read.AreaHelper;
import com.mcxx.modules.miniSalvation.order.param.QueryParam;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class QueryParamUtil implements ApplicationContextAware {
	private static AreaHelper areaHelper;
	private static final Logger logger = LoggerFactory.getLogger(OrderReadService.class);
	public static QueryParam setQueryParamDate(QueryParam param) {
		if (!CommonUtil.invalidArgs(param.getRecorddate())) {
			if (param.getRecorddate().indexOf("-") != -1) {
				String[] aa = param.getRecorddate().split("-");
				String startdate = aa[0] + "-" + aa[1] + "-" + aa[2].trim();
				String enddate = aa[3].trim() + "-" + aa[4] + "-" + aa[5] + " 23:59:59";
				param.setStartDate(startdate);
				param.setEnddate(enddate);
			}
		}
		if (CommonUtil.invalidArgs(param.getSendMoth())) {
			param.setNowMonth(DateUtil.getMonthString());
		}
		return param;
	}


	public static QueryParam setQueryParamCheckDate(QueryParam param) {
		if (!CommonUtil.invalidArgs(param.getCheckStartDate())) {
			if (param.getCheckStartDate().indexOf("-") != -1) {
				String[] aa = param.getCheckStartDate().split("-");
				String startDate = aa[0] + "-" + aa[1] + "-" + aa[2].trim();
				String endDate = aa[3].trim() + "-" + aa[4] + "-" + aa[5] + " 23:59:59";
				param.setCheckStartDate(startDate);
				param.setCheckEndDate(endDate);
			}
		}
		if (!CommonUtil.invalidArgs(param.getStartDate())) {
			if (param.getStartDate().indexOf("-") != -1) {
				String[] aa = param.getStartDate().split("-");
				if (aa.length > 3) {
					String startDate = aa[0] + "-" + aa[1] + "-" + aa[2] + " 00:00:00".trim();
					String endDate = aa[3].trim() + "-" + aa[4] + "-" + aa[5] + " 23:59:59";
					param.setCommitStartDate(startDate);
					param.setCommitEndDate(endDate);
				}
			}
		}
		if (!CommonUtil.invalidArgs(param.getSendMoth())) {
			if (param.getSendMoth().indexOf("-") != -1) {
				String[] aa = param.getSendMoth().split("-");
				String startDate = aa[0].trim();
				String endDate = aa[1].trim();
				param.setSendMoth(startDate);
				param.setEndMoth(endDate);
			}
		}
		if (CommonUtil.invalidArgs(param.getSendMoth())) {
			param.setNowMonth(DateUtil.getMonthString());
		}
		return param;
	}


	public static QueryParam setQueryParamLike(QueryParam param) {
		if (!CommonUtil.invalidArgs(param.getFullname())) {
			String ss = param.getFullname();
			ss = ss.trim();
			param.setFullname(ss + "%");
		}
		if (!CommonUtil.invalidArgs(param.getIdcard())) {
			String ss = param.getIdcard();
			ss = ss.trim();
			param.setIdcard(ss);
		}
		return param;
	}


	public static String setAreaCodeByLevel(String areaCode, Integer areaCodeLevel){
		if(null == areaCodeLevel) {
			return areaCode;
		}
		if(areaCodeLevel == 1){

			return areaCode.substring(0,2);
		}
		if(areaCodeLevel == 2){

			return areaCode.substring(0,4);
		}
		if(areaCodeLevel == 3){

			return areaCode.substring(0,6);
		}
		if(areaCodeLevel == 4){

			return areaCode.substring(0,9);
		}
		if(areaCodeLevel == 5){

			return areaCode;
		}

		return areaCode;
	}


	public static String subAreaCode(String areaCode){
		return areaCode != null ? areaCode.replaceAll("0+$", "") : null;
	}


	public static void setBaseParamValue(BaseParam param, UserAccountVo accountVo) {
		logger.info("start setBaseParamValue1");
		if(CommonUtil.invalidArgs(accountVo)){
			throw new BizException("-1", "请先登录");
		}
		logger.info("start setBaseParamValue2");

		String areaCode = StringUtils.isEmpty(param.getAreaCode()) ? accountVo.getAreaCode() : param.getAreaCode();

		List<TaAreaPo> userAreaPos = accountVo.getUserAreas();

		String orgId = accountVo.getOrgId();
		logger.info("accountVo={}", JSONUtils.toJSONString(accountVo));
		logger.info("accountVo.getAreaLevel:{}", accountVo.getAreaLevel());
		param.setUserLevel(Integer.parseInt(accountVo.getAreaLevel()));

		logger.info("start setBaseParamValue4:"+areaCode);

		AreaHelper.AreaPo areaPo = areaHelper.getAreaByCode(areaCode);
		if(null != areaPo)
			logger.info("start setBaseParamValue5:"+ JSON.toJSONString(areaPo));
		else
			logger.info("start setBaseParamValue5 null");
		if (areaPo != null) {
			Integer areaLevel = areaPo.getAreaLevel();
			logger.info("start setAreaCodeByLevel");
			areaCode = setAreaCodeByLevel(areaCode, areaLevel);
			logger.info("end setAreaCodeByLevel");
		}

		logger.info("start setBaseParamValue6 map");

		String userAreas = "";
		if (userAreaPos != null && userAreaPos.size() > 0) {
			userAreas = userAreaPos.stream().map(TaAreaPo::getAreaCode).collect(Collectors.joining(","));
		}


		param.setAreaCode(areaCode);
		param.setUserAreas(userAreas);
		param.setOrgId(orgId);
		logger.info("start setBaseParamValue7 end");
	}


	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		areaHelper = context.getBean(AreaHelper.class);
	}
}
